package hw2.cs236369.parsing;

import hw2.cs236369.interfaces.IAuthor;
import hw2.cs236369.interfaces.IDBLP;
import hw2.cs236369.interfaces.IPaper;

import java.io.IOException;
import java.util.Collection;

import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

/**
 * DBLP XML's Parser
 * 
 * @author gil
 * 
 */
public class DBLParser implements IDBLP {
	final private String		_regex;
	final private DBLPHandler	_handler;

	/**
	 * Creates a DBLP Parser and parse the given XML.
	 * 
	 * @param path The path to the DBLP's XML
	 * @param regex The regular expression to match authors' names
	 */
	public DBLParser(String path, String regex) {
		XMLReader reader;
		_regex = regex;
		_handler = new DBLPHandler(_regex);
		try {
			reader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
			//reader.setFeature("http://xml.org/sax/features/validation", false);
			reader.setContentHandler(_handler);
			reader.parse(path);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Collection<IAuthor> getFilteredAuthors() {
		return _handler.getAuthors();
	}

	@Override
	public Collection<IPaper> getPapers(IAuthor author) {
		return _handler.getAuthorsPapers(author);
	}
}
